package com.morehair.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description:
 * @Author raohui
 * @Date 2021/6/9 7:01
 */
public class DateUtils {

    private static DateFormat secondFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static DateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");

    public static Date String2Date(String source) {
        if (source == null) {
            return null;
        }
        source = source.trim();
        Date parse = null;
        try {
            if (source.length() == 10) {
                parse = dayFormat.parse(source);
            } else if (source.length() == 19) {
                parse = secondFormat.parse(source);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    public static String Date2String(Date date, Boolean accurateToSecond) {
        if (date == null) {
            return null;
        }
        String format;
        if (accurateToSecond) {
            format = secondFormat.format(date);
        } else {
            format = dayFormat.format(date);
        }
        return format;
    }

}
